package com.xueersi.interactivevideoeditor.model
{
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.events.CollectionEvent;

	[Bindable]
	public class InteractiveVideo
	{
		public var sectionID:int;
		public var name:String;
		public var videoPath:String;
		public var interactiveframes:ArrayCollection;
		
		/**
		 * unit by second	 
		 **/
		public var videoLength:uint;
		
		public function InteractiveVideo()
		{
			interactiveframes = new ArrayCollection();
//			interactiveframes.addEventListener(CollectionEvent.COLLECTION_CHANGE,onCollectionChange);
		}
		
		public function addInteractionFrame(value:InteractiveFrame):void
		{
			if(isContain(value))
			{
				trace("已经包含对应的元素。。。");
			}else
			{
				
				for each(var interactiveFrame:InteractiveFrame in interactiveframes)
				{
					if(interactiveFrame.time > value.time)
					{
						var index:int = interactiveframes.getItemIndex(interactiveFrame);
						value.frameLabel = "第" + (index + 1 ) + "帧";
						interactiveframes.addItemAt(value,index);
						
						return ;
					}
				}
				value.frameLabel = "第"+ (interactiveframes.length + 1) +"帧";
				interactiveframes.addItem(value);

			}	
		}
		
		public function removeInteractionFrame(value:InteractiveFrame):void
		{
			if(isContain(value))
			{
				var target:InteractiveFrame = isContain(value);
				var index:int = interactiveframes.getItemIndex(target);
				interactiveframes.removeItemAt(index);
			}else
			{
				trace("没有找到对应的元素。。");
			}	
		}
		
		public function isContain(value:InteractiveFrame):InteractiveFrame
		{
			if(!value)
			{
				trace("找不到对应的frame。。。");
				return null;
			}	

			return searchInteractiveFrame(value.time);
		}
		
		public function searchInteractiveFrame(value:int):InteractiveFrame
		{
			for each(var interFrame:InteractiveFrame in interactiveframes)
			{
				if(interFrame.time == value)
				{
					return interFrame;
				}	
			}
			return null;
		}
		
		public function getInteractiveFrameByTime(value:int):InteractiveFrame
		{
			var interactiveFrame:InteractiveFrame = searchInteractiveFrame(value);
			if(!interactiveFrame)
			{
				interactiveFrame = new InteractiveFrame();
				interactiveFrame.time = value;
			}
			
			return interactiveFrame;
		}	
		
		private function onCollectionChange(event:CollectionEvent):void
		{	
//			var sort:Sort = new Sort();
//			sort.fields = [new SortField("time",false,false,true)];
//			interactiveframes.sort = sort;
			interactiveframes.refresh();
		}	
	}
}